package com.Glow.reggie.controller;

import com.Glow.reggie.common.R;
import com.Glow.reggie.dto.SetmealDto;
import com.Glow.reggie.entity.Category;
import com.Glow.reggie.entity.Setmeal;
import com.Glow.reggie.entity.SetmealDish;
import com.Glow.reggie.service.CategoryService;
import com.Glow.reggie.service.SetmealDishService;
import com.Glow.reggie.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author Glow
 * @Date 2022-08-03 10:14:01
 * @Description 套餐管理控制层
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    @RequestMapping("page")
    @GetMapping
    public R<Page> page(int page,int pageSize,String name){
            Page<Setmeal> pageInfo = new Page<>(page,pageSize);
            Page<SetmealDto> setmealDtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
       setmealService.page(pageInfo,queryWrapper);
        BeanUtils.copyProperties(pageInfo,setmealDtoPage,"records");
        List<Setmeal>list = pageInfo.getRecords();
        List<SetmealDto> setmealDtoList = list.stream().map(item->{
            SetmealDto setmealDto = new SetmealDto();
            Long categoryId = item.getCategoryId();
            Category category = new Category();
            category = categoryService.getById(categoryId);
            if(category!=null)
            {
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            BeanUtils.copyProperties(item,setmealDto);
            return setmealDto;

        }).collect(Collectors.toList());
//        log.info("setmeal:{}",pageInfo);
        setmealDtoPage.setRecords(setmealDtoList);
        return R.success(setmealDtoPage);
    }

    @PostMapping
    @CacheEvict(value ="setmealCache",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto)
    {
        setmealService.save(setmealDto);
        return R.success("添加成功");
    }
    @GetMapping("{id}")
    public R<SetmealDto> getSetmealById(@PathVariable Long id)
    {
        log.info("id:{}",id);
        Setmeal setmeal = new Setmeal();
        setmeal = setmealService.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        List<SetmealDish> setmealDishes = null;
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        setmealDishes = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(setmealDishes);

        return R.success(setmealDto);
    }
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto)
    {
        setmealService.update(setmealDto);
      return R.success("修改成功！");
    };
//    @DeleteMapping
//    public R<String> remove(String ids)
//    {
//        setmealService.remove(ids);
//        return R.success("删除成功！");
//    }
    @PostMapping("status/{status}")
    public R<String> status(@PathVariable Integer status, String ids)
    {
        setmealService.status(status,ids);
        return R.success("修改成功！");
    }

    /**
     * 获取套餐信息
     * @param setmeal
     * @return
     */
    @GetMapping("list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    //因为返回的是R对象，需要对R加入序列化接口R.serializable
    public R<List<Setmeal>> list(Setmeal setmeal)
    {
        LambdaQueryWrapper<Setmeal>queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
    @DeleteMapping
    //删除套餐的所有缓存
    @CacheEvict(value ="setmealCache",allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids)
    {
        log.info("ids:{}",ids);
        setmealService.removeWithDish(ids);
        return R.success("数据删除成功！");
    }

}
